package history

/**
 * @Description: 数组的全排列
 * @Keyword:
 * @Author: kami
 * @Date: 2022/8/11 16:11
 **/
func subNums(nums []int) [][]int {
	var res [][]int

	var traceBack func(nums, cur []int, idx int)

	traceBack = func(nums, cur []int, idx int) {

		var tmp = make([]int, len(cur))
		copy(tmp, cur)
		res = append(res, tmp)

		for i := idx; i < len(nums); i++ {
			cur = append(cur, nums[i])
			traceBack(nums, cur, i+1)
			cur = cur[0 : len(cur)-1]
		}
	}

	traceBack(nums, []int{}, 0)
	return res
}

/**
 * @Description: 求平方根
 * @Keyword: 携程三面
 * @Author: kami
 * @Date: 2022/8/12 13:51
 **/
func sqrt(num float64) float64 {
	var pos = 0.01
	var right = num
	var left = 0.0
	for left < right {
		var mid = (left + right) / 2.0
		var sq = mid * mid
		var diff = sq - num
		if getAbs(diff) < pos {
			return mid
		}
		if diff > 0 {
			right = mid
		} else {
			left = mid
		}
	}
	return -1.0
}

func getAbs(num float64) float64 {
	if num < 0 {
		return -num
	}
	return num
}
